<%@ page import="br.com.rosena.model.bean.persistente.Persistente" %>
<%@ page import="br.com.rosena.control.json.JSONRosena" %>
<%@ page import="br.com.rosena.model.bean.persistente.Classe" %>
<%@ page import="br.com.rosena.model.bean.persistente.Atributo" %>
<%@ page import="org.json.JSONObject" %>

<%
    String strJson = request.getParameter("toStringJson") != null ? request.getParameter("toStringJson") : "";
    String nomeClasse = (request.getParameter("nomeClasse") != null ? request.getParameter("nomeClasse") : "");
    String id = (request.getParameter("id") != null ? request.getParameter("id") : "");
    try {

        if (strJson.equals("")) {
            JSONRosena json = new JSONRosena(new Persistente(nomeClasse));
            json.setNomeClasse(nomeClasse);
            if(!id.equals("") ){
                json = new JSONRosena(new Persistente(nomeClasse,new Long(id).longValue()));
            }
            out.print(json.getJSONRosena());
        } else {
            JSONObject objJSONPersistente = new JSONObject(strJson);
            Persistente objPersistente = new Persistente(nomeClasse);
            if(!objJSONPersistente.getString("id").equals("0") ){
                objPersistente = new Persistente(nomeClasse,new Long(objJSONPersistente.getString("id")).longValue());
            }
            Classe objClasse = objPersistente.getClasse();
            Atributo lstAtributo[] = objClasse.getArrayAtributos();

            for (int i = 0; i < lstAtributo.length; i++) {
                Atributo atributo = lstAtributo[i];
                String tipoAtributo = atributo.getTipo();
                if (tipoAtributo.equals(Atributo.String)) {
                    objPersistente.setString(atributo.getNome(),objJSONPersistente.getString(atributo.getNome()));
                } else if (tipoAtributo.equals(Atributo.Double)) {
                    objPersistente.setDouble(atributo.getNome(),br.com.rosena.util.Util.parseDouble(objJSONPersistente.getString(atributo.getNome()),"0.00") );
                } else if (tipoAtributo.equals(Atributo.Integer)) {
                    objPersistente.setInt(atributo.getNome(),objJSONPersistente.getInt(atributo.getNome()));
                } else if (tipoAtributo.equals(Atributo.Boolean)) {
                    objPersistente.setBoolean(atributo.getNome(),objJSONPersistente.getBoolean(atributo.getNome()));
                } else if (tipoAtributo.equals(Atributo.Time)) {
                    java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()) ,"HH:mm");
                    objPersistente.setTime(atributo.getNome(),new java.sql.Time(dt.getTime()));
                } else if (tipoAtributo.equals(Atributo.Date)) {
                    java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()) ,"dd/MM/yyyy");
                    objPersistente.setDate(atributo.getNome(),new java.sql.Date(dt.getTime()));
                } else if (tipoAtributo.equals(Atributo.TimeStamp)) {
                    java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()) ,"HH:mm:ss");
                    objPersistente.setTime(atributo.getNome(),new java.sql.Time(dt.getTime()));
                } else if (tipoAtributo.contains(Atributo.XML)) {
                    if (!atributo.getConsultaDoAtributo().equals(Atributo.XML_LIST)) {
                        if(objJSONPersistente.getLong(atributo.getNome()) > 0){
                            objPersistente.setPersistente(atributo.getNome(), new Persistente(atributo.getTipo().replace(".xml",""),objJSONPersistente.getLong(atributo.getNome())) );
                        }
                    }
                }
            }

            objPersistente.invocarMetodo(request.getParameter("comando") );//invocar comando
            if(objPersistente.getId() > 0){
                objJSONPersistente.put("id",objPersistente.getId() );
            }else{
                objJSONPersistente.put("id",0);
            }
            objJSONPersistente.put("error","");
            out.print(objJSONPersistente);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        JSONObject objJSONPersistente = new JSONObject(strJson);
        Persistente objPersistente = new Persistente(nomeClasse);
        if(!objJSONPersistente.getString("id").equals("0") ){
            objPersistente = new Persistente(nomeClasse,new Long(objJSONPersistente.getString("id")).longValue());
        }

        Classe objClasse = objPersistente.getClasse();
        Atributo lstAtributo[] = objClasse.getArrayAtributos();

        for (int i = 0; i < lstAtributo.length; i++) {
            Atributo atributo = lstAtributo[i];
            String tipoAtributo = atributo.getTipo();
            if (tipoAtributo.equals(Atributo.String)) {
                objPersistente.setString(atributo.getNome(),objJSONPersistente.getString(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Double)) {
                objPersistente.setDouble(atributo.getNome(),br.com.rosena.util.Util.parseDouble(objJSONPersistente.getString(atributo.getNome()),"0.00") );
            } else if (tipoAtributo.equals(Atributo.Integer)) {
                objPersistente.setInt(atributo.getNome(),objJSONPersistente.getInt(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Boolean)) {
                objPersistente.setBoolean(atributo.getNome(),objJSONPersistente.getBoolean(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Time)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()) ,"HH:mm");
                objPersistente.setTime(atributo.getNome(),new java.sql.Time(dt.getTime()));
            } else if (tipoAtributo.equals(Atributo.Date)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()) ,"dd/MM/yyyy");
                objPersistente.setDate(atributo.getNome(),new java.sql.Date(dt.getTime()));
            } else if (tipoAtributo.equals(Atributo.TimeStamp)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()) ,"HH:mm:ss");
                objPersistente.setTime(atributo.getNome(),new java.sql.Time(dt.getTime()));
            } else if (tipoAtributo.contains(Atributo.XML)) {
                if (!atributo.getConsultaDoAtributo().equals(Atributo.XML_LIST)) {
                    if(objJSONPersistente.getLong(atributo.getNome()) > 0){
                        objPersistente.setPersistente(atributo.getNome(), new Persistente(atributo.getTipo().replace(".xml",""),objJSONPersistente.getLong(atributo.getNome())) );
                    }
                }
            }
        }
        if(objPersistente.getId() > 0){
            objJSONPersistente.put("id",objPersistente.getId() );
        }else{
            objJSONPersistente.put("id",0);
        }
        objJSONPersistente.put("error",ex.getMessage().replace("java.lang.Exception:","") );//incluir error
        out.print(objJSONPersistente);
    }
%>
